/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
      if (root == NULL) {
        return true;
      }
      return dfs(root->left, root->right);
    }

    bool dfs(TreeNode *lt, TreeNode *rt) {
      /*if (l == NULL && r == NULL) {
        return true;
      } else if (l == NULL || r == NULL) {
        return false;
      }
      if (l->val != r->val) {
        return false;
      }
      return dfs(l->left, r->right) &&
             dfs(l->right, r->left);*/

      stack<pair<TreeNode *, TreeNode *> > stk;
      stk.push(make_pair(lt, rt));

      while (!stk.empty()) {
        TreeNode *l = stk.top().first;
        TreeNode *r = stk.top().second;
        stk.pop();
        if (l == NULL && r == NULL) {
          continue;
        } else if (l == NULL || r == NULL) {
          return false;
        }
        if (l->val != r->val) {
          return false;
        }
        stk.push(make_pair(l->right, r->left));
        stk.push(make_pair(l->left, r->right));
      }
      return true;
    }
};
